//
//  XGLNavigationController.swift

//

import UIKit
import MXCommonModule

@objc protocol NavigationProtocol {
    @objc optional func navigationShouldPopMethod() -> Bool
}

extension UIViewController: NavigationProtocol {
    func navigationShouldPopMethod() -> Bool {
        return true
    }
}

@available(iOS 13.0, *)
var navigationBarAppearance = UINavigationBarAppearance()


class XGLNavigationController: UINavigationController,UINavigationControllerDelegate{
    
    var popDelegate:UIGestureRecognizerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //        self.interactivePopGestureRecognizer?.delegate = self
        // Do any additional setup after loading the view.
        
        let backImg: UIImage = imageWithName("icon_back_black")
            .withRenderingMode(.alwaysOriginal)
        if #available(iOS 13.0, *) {
            navigationBarAppearance.backgroundColor = .white
            navigationBarAppearance.setBackIndicatorImage(backImg, transitionMaskImage: backImg)
            navigationBarAppearance.backButtonAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: -300, vertical: 0)
            navigationBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.hexColor("#202122"),.font: UIFont.pingfangSC_Regular(fontSize: 18.0)]
            self.navigationBar.scrollEdgeAppearance = navigationBarAppearance
            self.navigationBar.standardAppearance = navigationBarAppearance
            
        } else {
            // Fallback on earlier versions
            self.navigationBar.barTintColor = .white
            self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.hexColor("#202122"),.font: UIFont.pingfangSC_Regular(fontSize: 18.0)]
            UINavigationBar.appearance()
                .backIndicatorImage = backImg
            UINavigationBar.appearance()
                .backIndicatorTransitionMaskImage = backImg
            let buttonItem = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self])
            var offset = UIOffset()
            offset.horizontal = -300
            buttonItem.setBackButtonTitlePositionAdjustment(offset, for: .default)
        }
        
        
        self.interactivePopGestureRecognizer?.delegate = self
        self.delegate = self
    
    }
    
    
    
    override func pushViewController(_ viewController:UIViewController, animated:Bool) {

        if self.viewControllers.count > 0 {

            viewController.hidesBottomBarWhenPushed = true

        }

        super.pushViewController(viewController, animated: animated)

    }
    
    
    //MARK: - UIGestureRecognizerDelegate
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {

    }
    
}


extension XGLNavigationController: UIGestureRecognizerDelegate,UINavigationBarDelegate{
    
    func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
        
        if viewControllers.count < (navigationBar.items?.count)! {
            return true
        }
        var shouldPop = false
        let vc: UIViewController = topViewController!
        if vc.responds(to: #selector(navigationShouldPopMethod)) {
            shouldPop = vc.navigationShouldPopMethod()
        }
        if shouldPop {
            DispatchQueue.main.async {
                self.popViewController(animated: true)
            }
        } else {
            for subview in navigationBar.subviews {
                if 0.0 < subview.alpha && subview.alpha < 1.0 {
                    UIView.animate(withDuration: 0.25) {
                        subview.alpha = 1.0
                    }
                }
            }
        }
        return false
    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if children.count == 1 {
            return false
        } else {
            if topViewController?.responds(to: #selector(navigationShouldPopMethod)) != nil {
                return topViewController!.navigationShouldPopMethod()
            }
            return true
        }
    }
}

extension XGLNavigationController {

    open override var childForStatusBarStyle: UIViewController? {
        return viewControllers.last
    }
}
